#!/bin/bash
# 
# Here's a sample test to learn a little about bashWebTest. I pity the fool 
# who reads this. You must be in a desperate place. 
# 
#############################################################################

# This spacer is just for formatting purposes... 
#  $testname is the filename
#  $testid is a code for your tracking purposes. Use whatever you want. 
#  $testdescription 
# This info is shown in the header and footer of the test results. 
#

echo "======================================================================"
testid=0000
export spacer="\t\t"
testname=$0
#"Site Pages"
testdescription="Show a few of the basic functions of \
bashWebTest. More info on http://code.google.com/p/bashwebtest/ "
echo $testname [testid=$testid]
echo TEST: $testdescription on $server
echo "START"


# These includes bring in the functions used by bwt. 
# read through this to see what the tool will do for you. 
# 
. ./base_functions.sh
. ./sample_functions.sh


# Set up the server you want to test. Might be able to move this into
# base functions afterall. 

initServer $1

# Most tests should start from a clean place. This function 
# clears the cookies.txt file. 

clearCookies

# Get the homepage and assert some text is in the response. 
#  1. make the request to $server

getPage /

# At this point the response from $server is in a variable called 
# well, $response. You can 'echo $response' if you care to see it. 
# At this point let's move on with the test. 
#  2. check for the presence of your string in $response: 

assert "BorkFactor"

# get the homepage and assert some text is not in the response. 
# Note that you can aim several assertions at a single request. 
#  3. multiple tests, checking for presence and absence of strings. 
#     Note also that curl will pull the http header info into $response. 
#     You can assert cookies, redirects, etc. 

getPage /netscrap.cfm
assertNot "<title></title>"
assert "HTTP/1.1 200 OK"
assert "NetScrap"

# get a page containing the build version. Throw a warning if the
# version doesn't match. 
#  4. Throw a warning for tests you don't want to write a bug about. 
#     I'm leaving this one broken for now. 

getPage /version
assertWarn "55"

# Test the search feature. 
#  5. post data easily. Ok, maybe this is a real pain. 

postPage "netscrap_search.cfm" \
	"criteria=greacen" \
	"Netscrap_Search=Search"
assert Score: dunno.
assert Holiday Email from Dad

getPage "/netscrap_detail.cfm?scrap_id=3"
assert "dashiki"

# Loop through a list of terms and search. 
#  6. Drive that last test with some data. Fun!
#     The test is bash, so take advantage of its 
#     meager facilities. 

searchterms=( greacen money food )
for term in ${searchterms[@]}
do
   echo $term
		postPage "netscrap_search.cfm" \
			"criteria=$term" \
			"Netscrap_Search=Search"
		assert $term
done

# xhtml validation is worth automating. 
#  7. perform xhtml validation on a page. 
#     See the results in a directory called results (of course)
#     Does this violate the w3c TOS? They'll ban your IP if you 
#     run this one too frequently. 

validatePage netscrap.cfm

echo "DONE"
echo 

# This function summarizes and displays the results of your test. 
#   8. Try re-running the test and grep for RES: 
#      ./testSample.sh | grep RES
#      and you'll only see the results. 

results

echo ""
echo ""
echo "======================================================================"
echo ""
echo ""


exit 0

